package com.mock.ems.controller.config;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.mock.base.util.DateUtil;
import com.mock.base.util.StringUtil;
import com.mock.ems.common.JsonResult;

import java.util.*;

public class TrainConfigController extends Controller {

    public void index(){

        Record trainConfig = Db.findFirst("select id from train_config order by id desc");
        if(trainConfig != null){
            setAttr("configId",trainConfig.getLong("id"));
        }

        renderJsp("/page/config/train/train_index.jsp");
    }

    public void config(){

        String id = getPara("id");

        Record trainConfig = Db.findById("train_config",id);
        if(trainConfig != null){
            trainConfig.set("banners",trainConfig.getStr("banners").split(","));
            trainConfig.set("urls",trainConfig.getStr("urls").split(","));
            trainConfig.set("btype",trainConfig.getStr("btype").split(","));
            trainConfig.set("stype",trainConfig.getStr("stype").split(","));
            setAttr("trainConfig",trainConfig);
        }

        renderJsp("/page/config/train/train_config.jsp");
    }

    @Before(Tx.class)
    public void configSave(){
        String id = getPara("id");

        //处理banners
        String banners = getPara("banners");
        String urls = getPara("bannerUrls");

        String btype = getPara("btype");
        String stype = getPara("stype");

        Record trainConfig = Db.findById("train_config",id);
        String oldBtype = trainConfig.getStr("btype");
        String oldStype = trainConfig.getStr("stype");

        //大类修改，更新train
        if(!btype.equals(oldBtype)){

            String[] btypes = btype.split(",");
            String[] btypeArr = oldBtype.split(",");

            for (int i = 0;i < btypeArr.length;i++){

                //若大类名称修改，更新article大类名称
                if(!btypes[i].equals(btypeArr[i])){
                    Db.update("update train set btype = ? where btype = ?",btypes[i],btypeArr[i]);
                }
            }

            trainConfig.set("btype",btype);
        }

        //小类有修改,更新train
        if(!stype.equals(oldStype)){

            String[] stypes = stype.split(",");
            String[] stypeArr = oldStype.split(",");

            for (int i = 0;i < stypeArr.length;i++) {

                //若小类名称修改，更新article小类名称
                if (!stypes[i].equals(stypeArr[i])) {
                    Db.update("update train set stype = ? where stype = ?", stypes[i], stypeArr[i]);
                }
            }

            trainConfig.set("stype",stype);
        }

        trainConfig.set("banners",banners).set("urls",urls);
        Db.update("train_config",trainConfig);

        renderJson(new JsonResult().status("000").show("保存成功"));
    }

    public void article(){
        renderJsp("/page/config/train/train_article.jsp");
    }

    public void pagination() {

        int pageNumber = getParaToInt("pageNumber");
        int pageSize = getParaToInt("pageSize");

        StringBuilder exceptSelectSQL = new StringBuilder(" from train where 1 = 1 ");
        List<Object> params = new ArrayList<Object>();

        Date begin_time = getParaToDate("begin_time");
        Date end_time = getParaToDate("end_time");

        if (null != begin_time && end_time == null) {
            exceptSelectSQL.append(" and begin_time >= ? ");
            params.add(begin_time);
        }

        if (null == begin_time && end_time != null) {
            exceptSelectSQL.append(" and begin_time <= ? ");
            params.add(end_time);
        }

        if (null != begin_time && null != end_time) {
            exceptSelectSQL.append(" and begin_time >= ? and begin_time <= ? ");
            params.add(begin_time);
            params.add(end_time);
        }

        exceptSelectSQL.append(" order by update_time desc ");
        Page<Record> recordList = null;
        if (params.size() == 0) {
            recordList = Db.paginate(pageNumber, pageSize, "select * ", exceptSelectSQL.toString());
        }else {
            recordList = Db.paginate(pageNumber, pageSize, "select * ", exceptSelectSQL.toString(),params.toArray());
        }

        Map<String, Object> returnMap = new HashMap<String, Object>();
        returnMap.put("total", recordList.getTotalRow());
        returnMap.put("rows", recordList.getList());
        renderJson(returnMap);
    }

    public void detail(){

        String trainId = getPara("id");

        Record record = Db.findById("train",trainId);
        setAttr("record",record);

        renderJsp("/page/config/train/train_detail.jsp");
    }

    public void edit(){

        String id = getPara("id");
        Record trainConfig = Db.findFirst("select * from train_config order by id desc");
        if(StringUtil.isNotEmpty(id)){
            setAttr("record",Db.findById("train",id));
        }
        setAttr("btypes",trainConfig.getStr("btype"));
        setAttr("stypes",trainConfig.getStr("stype"));
        renderJsp("/page/config/train/article_edit.jsp");
    }

    @Before(Tx.class)
    public void save(){
        String id = getPara("id");
        Record train = new Record();
        if (StringUtil.isNotEmpty(id)){
            train = Db.findById("train",id);
        }

        String btype = getPara("btype");
        String stype = getPara("stype");
        String banner = getPara("banner");
        String title = getPara("title");
        String description = getPara("description");
        String introduction = getPara("introduction");
        String price = getPara("price");
        String tags = getPara("tags");
        String content = getPara("content");

        train.set("btype",btype).set("stype",stype).set("banner",banner)
                .set("title",title).set("description",description).set("introduction",introduction)
                .set("price",price).set("tags",tags).set("content",content)
                .set("update_time", DateUtil.getDate());

        if (StringUtil.isNotEmpty(id)){
            Db.update("train",train);
        }else{
            train.set("create_time",DateUtil.getDate());
            Db.save("train",train);
        }

        renderJson(new JsonResult().status("000").show("保存成功"));

    }
}
